package com.gxuwz.smem.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.smem.beans.Permission;
import com.gxuwz.smem.beans.Role;
import com.gxuwz.smem.beans.UserLogin;
import com.gxuwz.smem.service.PermissionService;
import com.gxuwz.smem.service.RoleService;
import com.gxuwz.smem.service.UserLoginService;
import com.gxuwz.smem.service.impl.PermissionServiceImpl;
import com.gxuwz.smem.service.impl.RoleServiceImpl;
import com.gxuwz.smem.service.impl.UserLoginServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		//1.获取用户名密码
		int user_id=Integer.parseInt(request.getParameter("user_id"));
		String user_password = request.getParameter("user_password");
		 
		//2.创建service调用登录方法
		UserLoginService service = new UserLoginServiceImpl();
		UserLogin userlogin = service.login(user_id, user_password);
		//System.out.println(user_id+" "+user_password);
		
		if(userlogin != null) {
			int status = userlogin.getUser_status();
			   if(status == 1){
				   //System.out.println("登录成功");
				   PermissionService service1 = new PermissionServiceImpl();
				   Permission permission = service1.find(userlogin.getUser_id());
				   System.out.println(permission.getRole_id());
				   RoleService service2 = new RoleServiceImpl();
				   Role role = service2.find(permission.getRole_id());
				   String role_name = role.getRole_name();
				   
				   if(role_name.equals("学生")){
						request.setAttribute("userlogin",userlogin);
						request.getRequestDispatcher("/StudentLogin.jsp").forward(request, response);
				   }else if(role_name.equals("管理员")){
					   request.setAttribute("userlogin",userlogin);
						request.getRequestDispatcher("/AdminLogin.jsp").forward(request, response);
				   }else if(role_name.equals("班主任")){
					   request.setAttribute("userlogin",userlogin);
						request.getRequestDispatcher("/TeatherLogin.jsp").forward(request, response);
				   }else if(role_name.equals("值周人员")){
					   request.setAttribute("userlogin",userlogin);
						request.getRequestDispatcher("/MangerLogin.jsp").forward(request, response);
				   }else{
					   request.getRequestDispatcher("/login.jsp").forward(request, response);
				   }
				   
			   }else if(status == 0){
				   request.setAttribute("message", "账号已被冻结！");
				   request.getRequestDispatcher("/login.jsp").forward(request,response);
			   }else if(status == 2){
				   request.setAttribute("message", "账号已被删除！");
				   request.getRequestDispatcher("/login.jsp").forward(request, response);
			   }else{
				   request.setAttribute("message", "未知，请联系管理员！");
				   request.getRequestDispatcher("/login.jsp").forward(request, response);	
			   }
		}else{
			//System.out.println("用户名或密码错误");
			request.setAttribute("message", "用户名或密码错误！");
			request.getRequestDispatcher("/login.jsp").forward(request, response);	
		}

	}

}
